//
//  YTLocationManager.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/3/31.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTLocationManager.h"

@interface YTLocationManager ()

@property (strong, nonatomic) AMapLocationManager *locationManager;

@end

@implementation YTLocationManager

+ (instancetype)manager {
    static YTLocationManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[YTLocationManager alloc] init];
    });
    
    return manager;
}

- (void)singleUpdateLocation:(void (^)(CLLocation *location, AMapLocationReGeocode *regeocode))updateLocation {
    // 如果用户拒绝了地理位置的使用权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        [UIAlertView bk_showAlertViewWithTitle:@"友情提示" message:@"客观，您没有给小的使用位置权限呢" cancelButtonTitle:@"确定" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
            updateLocation(nil, nil);
        }];
    } else {
        _locationManager = [[AMapLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.locationTimeout = 5;
        [_locationManager requestLocationWithReGeocode:YES
                                       completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
            if (error) {
                NSLog(@"%@", error.description);
            } else {
                updateLocation(location, regeocode);
            }
        }];
    }
}

@end
